//
//  UrlManager.m
//  Four_Fit_New
//
//  Created by lanou on 16/7/14.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "UrlManager.h"
#import "FindModel.h"
#import "EatModel.h"
static UrlManager *manger = nil;
@interface UrlManager()<NSURLSessionDownloadDelegate>

@property (nonatomic,strong)NSURLSession *session;
@property (nonatomic,strong)NSURLSessionDownloadTask *task;
@property (nonatomic,copy)Downloading downLoading;
@property (nonatomic,copy)Complted complted;


@property (nonatomic,strong)NSString *filePath;///文件路劲

//@property (nonatomic,strong)NSMutableDictionary *dataSource;

@property (nonatomic,strong)NSMutableArray *dataSource;



@end


@implementation UrlManager
- (NSURLSession *)session{
    if (!_session) {
        _session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
    }
    return _session;
}
- (instancetype)initWithUrl:(NSString *)url withFilePath:(NSString *)filePath{
    self = [super init];
    if (self) {
        self.task = [self.session downloadTaskWithURL:[NSURL URLWithString:url]];
        self.filePath = [self.filePath stringByAppendingPathComponent:url];
    }
    return self;
}
///开始下载
- (void)startLoad{
    [self.task resume];
}

///正在下载
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
    if (self.downLoading) {
      
    self.downLoading(bytesWritten,100*totalBytesWritten/(float)totalBytesExpectedToWrite);
    }
}
///下载完成
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
    
    
   
    ///移动文件到缓存的地址（自定义）
    [[NSFileManager defaultManager]moveItemAtPath:location.path toPath:self.filePath error:nil];
    
    if (self.complted) {
        self.complted(self.filePath,@"wangzhi");
    }
    NSLog(@"%@",self.filePath);
    
}
///下载的时候和下载时要走的方法
- (void)downLoading:(Downloading)downloading didFinished:(Complted)completed{
    
    self.downLoading = downloading;
    self.complted = completed;
    
}






- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
+ (instancetype)creatUrlManager{
    if (manger == nil) {
        manger = [[UrlManager alloc]init];
    }
    return manger;
}
///组装网址
- (NSString *)packageURLWithNsArray:(NSArray *)arr andUrl:(NSString *)url{
    NSMutableString *urlResult = [NSMutableString string];
    [urlResult setString:url];
    for (int i=0;i<arr.count;i++) {
        FindModel *model = arr[i];
        [urlResult appendFormat:@"id=%@&",model.infoId];
    }
    
    NSRange range = {urlResult.length-1,1};
    
    [urlResult deleteCharactersInRange:range];
    NSLog(@"%@",urlResult);
    return urlResult;
}

///饮食数据请求
- (void)fetchEatingDataWithUrl:(NSString *)url{
    
   [self fetchDataWithUrl:url];
    NSLog(@"%@",self.dataSource);
   
    
    
}

///请求体
- (void)fetchDataWithUrl:(NSString *)url{
    
    
     [[QZRequestManager sharedManager] fetchDataWithUrl:url parameters:@{} method:@"GET" needToCaches:NO fetchData:^(NSData *data) {
       
         NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
         NSLog(@"%@",BigDic);
         for (NSDictionary *smallDic in BigDic[@"infos"]) {
              EatModel *model = [[EatModel alloc]init];
              [model setValuesForKeysWithDictionary:smallDic];
             [self.dataSource addObject:model];
         }
         
        
    } updateUI:^{
        
        
    }];
   
    
}
///返回数据组
- (NSArray *)backDataSource{
    return self.dataSource;
}









@end
